﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Data.Odbc;

public partial class DeliverGoodsManagement_ImportWindow : System.Web.UI.Page
{
    private List<Model.PAYTYPE> PayType
    {
        get
        {
            List<Model.PAYTYPE> payType = null;
            if (ViewState["PayType"] != null)
            {
                payType = (List<Model.PAYTYPE>)ViewState["PayType"];
            }
            return payType;
        }
        set
        {
            ViewState["PayType"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("700101");
        if (!IsPostBack)
        {
            BindDropDealType();
        }
    }

    private void BindDropDealType()
    {
        this.PayType = new BLL.PAYTYPE().GetAllList();
        dropPayType.DataSource = this.PayType;
        dropPayType.DataTextField = "PT_NAME";
        dropPayType.DataValueField = "PT_CODE";
        dropPayType.DataBind();
        ListItem item = new ListItem();
        item.Text = "";
        item.Value = "-1";
        dropPayType.Items.Insert(0, item);
    }

    protected void dropPayType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dropPayType.SelectedIndex != 0)
        {
            foreach (Model.PAYTYPE temp in this.PayType)
            {
                if (dropPayType.SelectedValue == temp.PT_CODE)
                {
                    lblAccount.Text = temp.PA_ACCOUNT;
                    break;
                }
            }
        }
        else
        {
            lblAccount.Text = "";
        }
    }

    protected void btnImport_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("700101", "00022"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        try
        {
            if (lblAccount.Text == "")
            {
                Share.Alert(this.Page, "请选择正确的支付方式和帐号！");
                return;
            }
            if (FileUpload1.HasFile)
            {
                string filePath = FileUpload1.PostedFile.FileName;
                string type = FileUpload1.PostedFile.ContentType;
                string filename = DateTime.Now.Ticks.ToString();
                string filenameExtension = filePath.Substring(filePath.LastIndexOf(".")).ToString().Trim();
                
                if (type == "application/vnd.ms-excel" || type=="text/plain")
                {
                    string absolutePath = Server.MapPath("../ExcelFileUpload/" + filename + filenameExtension);
                    FileUpload1.PostedFile.SaveAs(absolutePath);
                    if (File.Exists(absolutePath))
                    {
                        if (filenameExtension.ToLower() == ".csv")
                        {
                            string newDealLogId = "";
                            DataSet dst = new BLL.IMPORTBILL().CsvDataSource(Server.MapPath("../ExcelFileUpload/"), filename + filenameExtension);
                            if (dst.Tables[0].Rows.Count == 0)
                            {
                                Share.Alert(this.Page, "Excel文件内没有可导入记录！");
                                return;
                            }
                            string alert = "";
                            bool result = new BLL.IMPORTBILL().Import(dst, dropPayType.SelectedValue, lblAccount.Text, Share.UserValue(0), DateTime.Now, out newDealLogId, out alert);
                            if (result)
                            {

                                if (File.Exists(absolutePath))
                                {
                                    File.Delete(absolutePath);
                                }
                                Share.InsertLog("700101", "00022");
                                if (alert.Length > 0)
                                {
                                    Share.Alert(this.Page, alert);
                                    Response.Write("<script>alert('" + alert + "');window.location.href='DealLogDetail.aspx?code=" + newDealLogId + "'</script>");
                                }
                                else
                                {
                                    Response.Write("<script>alert('导入成功！');window.location.href='DealLogDetail.aspx?code=" + newDealLogId + "'</script>");
                                }
                            }
                            else
                            {
                                if (File.Exists(absolutePath))
                                {
                                    File.Delete(absolutePath);
                                }
                                Share.Alert(this.Page, "导入失败，请重新导入！");
                            }
                        }
                        else
                        {
                            Share.Alert(this.Page, "导入文件不是csv文件！");
                        }

                    }
                    else
                    {
                        Share.Alert(this.Page,"无法上传文件到服务器，请重新尝试！");
                    }
                }
                else
                {
                    Response.Write("<script>alert('文件格式错误，请选择Excel文件！');</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('请选择需导入的Excel文件！');</script>");
            }
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page,ex.Message);
        }
    }

    

    
}
